<template>
	<view>
		<view class="top_header" :style="{ position: isFixed ? 'fixed' : 'static'}">
			<!-- 头部 -->
			<!-- 	<view class="header">
				<icon type="search" size="16" color="#bbb">
				</icon>
				<input type="text" v-model="keyword" confirm-type="search" @confirm="search"/>
			</view> -->

			<!-- 导入公共组件searchBar -->
			<searchBar @search="enterSearch" :keyword="keyword"></searchBar>
			<!-- 过滤菜单 -->
			<view class="filter-menu">
				<view :class="{active: activeIndex === index}" v-for="(item, index) in menuList" :key="index" @click="selectMenu(index)">{{item}}
				</view>
			</view>
		</view>
		<!-- 商品列表 -->
		<view class="goods-list" :style="{marginTop:isFixed ? '220rpx' : '0'}">
			<view class="goods" v-for="(item, index) in goodsList" :key="index" @click="toItem(item.goods_id)">
				<image :src="item.goods_small_logo" alt=""></image>
				<view class="right">
					<view class="goods-name text-line2">{{ item.goods_name }}</view>
					<view class="price">
						￥
						<text>{{ item.goods_price }}</text>
						.00
					</view>
				</view>
			</view>
		</view>
		<!-- 添加提示 -->
		<view class="btm-line" v-if="islastPage"> -- 到底了！！ --
		</view>
	</view>

</template>
<script>
	import searchBar from '@/components/searchBar.vue';
	// 设置页容量
	const PAGE_SIZE = 6
	const HISTORY_LIST = 'history'
	export default {
		components: {
			searchBar
		},
		data() {
			return {
				menuList: ['综合', '销量', '价格'],
				// 当前选中的过滤栏的下标
				activeIndex: 0,
				// 输入框的关键字
				keyword: '',
				// 商品列表
				goodsList: [],
				// 设置已经到最后页
				islastPage: false,
				// 是否有请求在请求中，默认为false
				isRequesting: false,
				// top_header是否固定
				isFixed: true

			};
		},
		onLoad(options) {
			this.keyword = options.catName
			// 将当前历史记录加入至本地存储
			this.addHistory()
			this.queryGoodsList()
		},
		// 1、 设置下拉刷新
		onPullDownRefresh() {
			console.log('refresh');
			this.isFixed = false;
			this.search()
		},
		///2、 设置上拉刷新
		onReachBottom() {

			if (!this.islastPage) {
				// 加载更多数据
				// 添加页码
				pagenum++
				// 请求数据
				this.queryGoodsList()
			}
		},
		///3、 页面滚动时,设置头部固定
		onPageScroll() {
			this.isFixed = true;
		},

		methods: {
			// 1、获取到商品列表
			async queryGoodsList() {
				// 判断是否在有其他请求在请求中
				if (this.isRequesting) {
					return;
				}
				// 请求前设置状态为true
				this.isRequesting = true;
				let data = await this.$request({
					url: '/api/public/v1/goods/search',
					data: {
						query: this.keyword,
						pagenum: this.pagenum,
						pagesize: this.PAGE_SIZE
					},
					// 请求不需要loading
					showLoading: false,
				});

				// 请求结束后,设置请求状态为false
				this.isRequesting = false
				// this.goodsList = data.goods
				// 请求数据后 停止加载动画
				uni.stopPullDownRefresh()
				// 根据上拉刷新 将数据追加
				this.goodsList = [...this.goodsList, ...data.goods];

				//  如果刷新出来的数据比实际数据相等  就是最后一页
				if (data.total <= this.goodsList.length) {
					this.islastPage = true
				}

			},
			// 2、 选过滤栏菜单
			selectMenu(index) {
				this.activeIndex = index;
			},
			// 3、输入框触发搜索功能
			search() {
				// 步骤： 输入内容时，页码等于1， 进行搜索，并且清空列表
				this.pagenum = 1;
				this.goodsList = []
				this.queryGoodsList()
				this.islastPage = false;
			},

			// 4、点击商品进行跳转
			toItem(goodsid) {
				uni.navigateTo({
					url: '/pages/item/item?goodsid=' + goodsid
				})
			},
			// 5、回车触发搜索
			enterSearch(inputVal) {
				this.keyword = inputVal
				this.search()
				this.addHistory()
			},
			addHistory() {
				let historyList = uni.getStorageSync(HISTORY_LIST)
					// 插入到头部并去重
					let _historyList = [...new Set([this.keyword, ...historyList])];
					//存到storage
					uni.setStorageSync(HISTORY_LIST, _historyList);
				}
			}
		}
</script>
<style lang="less">
	.top_header {
		top: 0;
		width: 100%;
		background-color: #fff;
	}

	.header {
		height: 120rpx;
		padding: 30rpx 16rpx;
		box-sizing: border-box;
		background-color: #eee;
		position: relative;

		icon {
			position: absolute;
			top: 48rpx;
			left: 44rpx;
		}

		input {
			height: 60rpx;
			width: 100%;
			border-radius: 4rpx;
			background-color: #fff;
			padding-left: 80rpx;
			box-sizing: border-box;
		}
	}

	.header {
		height: 120rpx;
		padding: 30rpx 16rpx;
		box-sizing: border-box;
		background-color: #eee;
		position: relative;

		icon {
			position: absolute;
			top: 48rpx;
			left: 44rpx;
		}

		input {
			height: 60rpx;
			width: 100%;
			border-radius: 4rpx;
			background-color: #fff;
			padding-left: 80rpx;
			box-sizing: border-box;
		}
	}

	.filter-menu {
		display: flex;
		justify-content: space-around;
		align-items: center;
		height: 100rpx;

		view.active {
			color: #EB4450;
		}
	}

	.goods {
		border-top: 1rpx solid #ddd;
		height: 260rpx;
		box-sizing: border-box;
		padding: 0 20rpx;
		display: flex;
		align-items: center;

		image {
			width: 200rpx;
			height: 200rpx;
		}

		.right {
			flex: 1;
			margin-left: 26rpx;

			.price {
				color: #eb4450;
				font-size: 24rpx;
				margin-top: 80rpx;

				text {
					font-size: 36rpx;
				}
			}
		}
	}

	.btm-line {
		text-align: center;
	}
</style>
